package com.example.testcontract.bean;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by admin on 2014/9/19.
 */
public class QuickLocationRightTool extends View {
    private String[] letter;
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    private int choose = -1;
    private Paint paint = new Paint();
    private boolean showBkg = false;

    public QuickLocationRightTool(Context context) {
        super(context);
    }

    public QuickLocationRightTool(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public QuickLocationRightTool(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setLetter(String[] letter) {
        this.letter = letter;
    }

    public void setChoose(int choose) {
        this.choose = choose;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (showBkg)  canvas.drawColor(Color.parseColor("#10000000"));
        int height = getHeight();
        int width = getWidth();
        int len = letter.length;
        int singleHeight = height / len;
        for (int i = 0;i < len;i++) {
            paint.setColor(Color.BLACK);
            paint.setTextSize(20);
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            if (i == choose) {
                paint.setColor(Color.parseColor("#3399ff"));
                paint.setFakeBoldText(true);
            }
            float xPos = width / 2 - paint.measureText(letter[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(letter[i], xPos, yPos, paint);
            paint.reset();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        int oldChoose = choose;
        int c = (int) (y / getHeight() * letter.length);    //字母位置
//        OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;
                if (oldChoose != c && onTouchingLetterChangedListener != null) {
                    if (c >= 0 && c <= letter.length) {
                        onTouchingLetterChangedListener.onTouchingLetterChanged(letter[c]);
                        //choose = c;                  //处理重复
                        //invalidate();
                    }
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && onTouchingLetterChangedListener != null) {
                    if (c >= 0 && c < letter.length) {
                        Log.d("action move", "");
                        onTouchingLetterChangedListener.onTouchingLetterChanged(letter[c]);
                        //choose = c;
                        //invalidate();
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
                showBkg = false;
                choose = -1;                           //不刷新，让选中字母颜色改变
                //invalidate();
                break;

            default: break;
        }
        return true;
    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener)
    {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener
    {
        public void onTouchingLetterChanged(String s);
    }
}
